/*
Copyright (C) Ariff Ehsan, Abdul Hadi Fikri

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*  Author : Ariff Ehsan
 *  Date   : June 29, 2009
 */


#include <system.h>

timer_t app_timer;

void APP_TIMER_run(void);
void APP_ADC_readDone(uint16_t);

void APP_booted(){
	
	/* initialize leds */
	LEDS8_init();
	
	/* initilize printf */
	PRINTF_init();
	
	/* initialize and wire adc */
	ADC_init();
	ADC_reg(APP_ADC_readDone);
	
	/* initilize multiplexer output pin */
	DDRB |= (1<<PB2)|(1<<PB3)|(1<<PB4);
	SET_BIT(DDRD, PD7);
	
	/* enable multiplexer output */
	CLR_BIT(PORTD, PD7);
	
	PORTB &= 0xe3;
	PORTB |= (0<<2);
	
	/* register timer */
	TIMER_reg(&app_timer, APP_TIMER_run);
	
	/* start periodic timer */
	TIMER_startPeriodic(&app_timer, 1000);
}

void APP_TIMER_run(void) {
	LEDS8_toggle(0);
	ADC_read(7);
}

void APP_ADC_readDone(uint16_t result) {
	LEDS8_toggle(1);
	printf("testadc=%d\r", result);
}
